package nus.iss.laps.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import nus.iss.laps.general.RecordStatus;
import nus.iss.laps.model.User;

import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class UserDAO extends DAO implements IUser,RowMapper<User> {

	@Override
	public User mapRow(ResultSet arg0, int arg1) throws SQLException {
		User l_user = new User();
		l_user.setDefaultPage(arg0.getString("DefaultPage"));
		l_user.setLoginID(arg0.getString("LoginID"));
		l_user.setLoginName(arg0.getString("LoginName"));
		l_user.setPassword(arg0.getString("Password"));
		l_user.setType(arg0.getString("Type"));
		l_user.setUserStatus(arg0.getByte("UserStatus"));
		return l_user;
	}

	@Override
	public boolean addUser(User user) throws SQLException {
		query = "insert into " + table + " values(?, ?, ?, ?, ?, ?)";
		jdbcTemplate = new JdbcTemplate(dataSource);
		return jdbcTemplate.update(query, user.getLoginID(), user.getLoginName(), user.getPassword(), user.getUserStatus(), user.getType(), user.getDefaultPage()) > 0;
	}

	@Override
	public boolean updateUser(User user) throws SQLException {
		query = "update " + table + " set "
				+ " loginname = ?,"
				+ " type = ?,"
				+ " defaultpage = ?,"
				+ " password = ?"
				+ " where loginid = '" + user.getLoginID() + "'";
		jdbcTemplate = new JdbcTemplate(dataSource);
		return jdbcTemplate.update(query, user.getLoginName(), user.getType(), user.getDefaultPage(), user.getPassword()) > 0;
	}

	@Override
	public boolean deleteUser(String loginID) throws SQLException {
		query = "update " + table + " set userstatus = " + RecordStatus.delete +  " where loginid = '" + loginID + "'";
		jdbcTemplate = new JdbcTemplate(dataSource);
		return jdbcTemplate.update(query) > 0;
	}

	@Override
	public User getUser(String loginID) throws SQLException {
		try {
			query = getSelectQuery() + " where loginid = '" + loginID + "'";
			jdbcTemplate = new JdbcTemplate(dataSource);
			return jdbcTemplate.queryForObject(query, this);
		} catch (IncorrectResultSizeDataAccessException e) {
			return null;
		}
	}

	@Override
	public List<User> getAllUser() throws SQLException {
		query = getSelectQuery();
		jdbcTemplate = new JdbcTemplate(dataSource);
		return jdbcTemplate.query(query, this);
	}

	@Override
	public User isExisted(String loginID, String password)
			throws SQLException {
		User l_user = null;
		query = getSelectQuery() + 
				" where loginid = '" + loginID + "'" +
				" and userstatus <> " + RecordStatus.delete +
				" and password = '" + password + "'";
		jdbcTemplate = new JdbcTemplate(dataSource);
		try {
			l_user = jdbcTemplate.queryForObject(query, this);
		} catch (IncorrectResultSizeDataAccessException e) {
			return null;
		}
		return l_user;
	}

	@Override
	public boolean isExisted(String loginID) throws SQLException {
		if(getUser(loginID) != null)
			return true;
		return false;
	}

}
